package samfeng.erp.quant.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import samfeng.erp.quant.controller.admin.vo.*;
import samfeng.erp.quant.convert.QuantConvert;
import samfeng.erp.quant.dal.dataobject.QuantDO;
import samfeng.erp.quant.service.QuantService;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Api(tags = "管理后台 - 仓库计算记录")
@RestController
@RequestMapping("/stock/quant")
@Validated
public class QuantController {

    @Resource
    private QuantService quantService;

    @PostMapping("/create")
    @ApiOperation("创建仓库计算记录")
    @PreAuthorize("@ss.hasPermission('stock:quant:create')")
    public CommonResult<Long> createQuant(@Valid @RequestBody QuantCreateReqVO createReqVO) {
        return success(quantService.createQuant(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新仓库计算记录")
    @PreAuthorize("@ss.hasPermission('stock:quant:update')")
    public CommonResult<Boolean> updateQuant(@Valid @RequestBody QuantUpdateReqVO updateReqVO) {
        quantService.updateQuant(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除仓库计算记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('stock:quant:delete')")
    public CommonResult<Boolean> deleteQuant(@RequestParam("id") Long id) {
        quantService.deleteQuant(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得仓库计算记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('stock:quant:query')")
    public CommonResult<QuantRespVO> getQuant(@RequestParam("id") Long id) {
        QuantDO quant = quantService.getQuant(id);
        return success(QuantConvert.INSTANCE.convert(quant));
    }

    @GetMapping("/list")
    @ApiOperation("获得仓库计算记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('stock:quant:query')")
    public CommonResult<List<QuantRespVO>> getQuantList(@RequestParam("ids") Collection<Long> ids) {
        List<QuantDO> list = quantService.getQuantList(ids);
        return success(QuantConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得仓库计算记录分页")
    @PreAuthorize("@ss.hasPermission('stock:quant:query')")
    public CommonResult<PageResult<QuantRespVO>> getQuantPage(@Valid QuantPageReqVO pageVO) {
        PageResult<QuantDO> pageResult = quantService.getQuantPage(pageVO);
        return success(QuantConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/quantPage")
    @ApiOperation("获得仓库计算记录分页")
    @PreAuthorize("@ss.hasPermission('stock:quant:query')")
    public CommonResult<PageResult<QuantRespVO>> getQuantPageInProductPage(@Valid QuantPageReqVO pageVO) {
        PageResult<QuantDO> pageResult = quantService.getQuantPageInProductPage(pageVO);
        return success(QuantConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出仓库计算记录 Excel")
    @PreAuthorize("@ss.hasPermission('stock:quant:export')")
    @OperateLog(type = EXPORT)
    public void exportQuantExcel(@Valid QuantExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<QuantDO> list = quantService.getQuantList(exportReqVO);
        // 导出 Excel
        List<QuantExcelVO> datas = QuantConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "仓库计算记录.xls", "数据", QuantExcelVO.class, datas);
    }

    /**
     * 简单的quant vo分页
     * @param stockQuantSummarizingPageReqVO 分页对象
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PreAuthorize("@ss.hasPermission('stock:quant:query')")
    @GetMapping("/pageVo")
    public CommonResult<PageResult<StockQuantSummarizingPageReqVO>> getQuantPageVo(@Valid StockQuantSummarizingPageReqVO stockQuantSummarizingPageReqVO) {
        PageResult<StockQuantSummarizingPageReqVO> pageResult = quantService.getStockQuantSummarizingPageVO(stockQuantSummarizingPageReqVO);
        return success(pageResult);
    }

}
